/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scheduler;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JOptionPane;

/**
 *
 * @author Musikero
 */
public class LoginWindow extends javax.swing.JFrame {

    /**
     * Creates new form LoginWindow
     */
    public LoginWindow() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        Heading = new javax.swing.JLabel();
        IdNumberField = new javax.swing.JTextField();
        IdNumberLabel = new javax.swing.JLabel();
        PasswordLabel = new javax.swing.JLabel();
        PasswordField = new javax.swing.JPasswordField();
        UserTypeCombo = new javax.swing.JComboBox();
        LoginButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Musikero\\Pictures\\Nature_Beach_Relax_on_the_beach_016595_.jpg")); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jLayeredPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        Heading.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        Heading.setText("Attendance Checker");
        Heading.setBounds(10, 10, 212, 29);
        jLayeredPane1.add(Heading, new Integer(1));
        IdNumberField.setBounds(100, 50, 183, 20);
        jLayeredPane1.add(IdNumberField, new Integer(1));

        IdNumberLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        IdNumberLabel.setText("ID Number");
        IdNumberLabel.setBounds(10, 50, 65, 15);
        jLayeredPane1.add(IdNumberLabel, new Integer(1));

        PasswordLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        PasswordLabel.setText("Password:");
        PasswordLabel.setBounds(10, 80, 63, 15);
        jLayeredPane1.add(PasswordLabel, new Integer(1));
        PasswordField.setBounds(100, 80, 183, 20);
        jLayeredPane1.add(PasswordField, new Integer(1));

        UserTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Student", "Instructor" }));
        UserTypeCombo.setBounds(10, 130, 128, 20);
        jLayeredPane1.add(UserTypeCombo, new Integer(1));

        LoginButton.setText("Login");
        LoginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoginButtonActionPerformed(evt);
            }
        });
        LoginButton.setBounds(150, 130, 70, 23);
        jLayeredPane1.add(LoginButton, new Integer(1));

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });
        CancelButton.setBounds(240, 130, 80, 23);
        jLayeredPane1.add(CancelButton, new Integer(1));

        jLabel3.setIcon(new javax.swing.ImageIcon("C:\\Users\\Musikero\\Pictures\\Nature_Beach_Relax_on_the_beach_016595_.jpg")); // NOI18N
        jLabel3.setBounds(-6, -6, 360, 310);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoginButtonActionPerformed
        // TODO add your handling code here:
        Scanner scan = new Scanner(System.in);
        String userName;
        String password;
        String userType;
        String inputUserName;
        String inputPassword;
        
       
        userType = (String)UserTypeCombo.getSelectedItem();
        userName = IdNumberField.getText();
        switch (userType) {
            
            case "Student":
                 try{
                    String userNameBuffer;
                    String [] splitTextFile;
                    FileReader file = new FileReader("StudLogin.txt");
                    BufferedReader in = new BufferedReader(file); 
                    userNameBuffer = in.readLine();
                    boolean notFound = true;
                    
                    inputUserName = IdNumberField.getText();
                    inputPassword = new String(PasswordField.getPassword());
                    while((userNameBuffer = in.readLine()) != null){
                        splitTextFile = userNameBuffer.split(" ");
                        userName = splitTextFile[0];
                        password = splitTextFile[1];
                        if(inputUserName.equals(userName) && inputPassword.equals(password)){
                            this.setVisible(false);
                            StudentWindow studWindow = new StudentWindow();
                            studWindow.setVisible(true);
                            notFound = false;
                            break;
                        } else if(inputUserName == null){
                            InvalidPassword invalidPass;
                            invalidPass= new InvalidPassword();
                            invalidPass.setVisible(true);
                        }
                    }   
                  
                    if(notFound){
                            InvalidPassword invalidPass = new InvalidPassword();
                            invalidPass.setVisible(true);
                    }
                   
                break; //case break
                }catch(Exception fileNotFound){
                    JOptionPane.showMessageDialog(null, "Authorization text file is not found.");
                }  
            case "Instructor":
                  try{
                    String userNameBuffer;
                    String [] splitTextFile;
                    FileReader file;
                    file = new FileReader("InstructorLogin.txt");
                    BufferedReader in = new BufferedReader(file); 
                    userNameBuffer = in.readLine();
                    boolean notFound = true;
                    
                    inputUserName = IdNumberField.getText();
                    inputPassword = new String(PasswordField.getPassword());
                    while((userNameBuffer = in.readLine()) != null){
                        splitTextFile = userNameBuffer.split(" ");
                        userName = splitTextFile[0];
                        password = splitTextFile[1];
                        if(inputUserName.equalsIgnoreCase(userName) && inputPassword.equals(password)){
                            this.setVisible(false);
                            InstructorWindow instrWindow = new InstructorWindow();
                            instrWindow.setVisible(true);
                            notFound = false;
                            break;
                        } else if(inputUserName == null){
                            InvalidPassword invalidPass;
                            invalidPass = new InvalidPassword();
                            invalidPass.setVisible(true);
                        }
                    }   
                  
                    if(notFound){
                            InvalidPassword invalidPass = new InvalidPassword();
                            invalidPass.setVisible(true);
                    }
                   
                break; //case break
                }catch(Exception A){
                    A.printStackTrace();
                    //JOptionPane.showMessageDialog(null, "Authorization text file is not found.");
                }
        }//end of switch
        
        
     
    }//GEN-LAST:event_LoginButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_CancelButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoginWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoginWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoginWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoginWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LoginWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JLabel Heading;
    private javax.swing.JTextField IdNumberField;
    private javax.swing.JLabel IdNumberLabel;
    private javax.swing.JButton LoginButton;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JLabel PasswordLabel;
    private javax.swing.JComboBox UserTypeCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLayeredPane jLayeredPane1;
    // End of variables declaration//GEN-END:variables
}
